-- NewsPaperView

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.NewsPaperDetailsScene

function ClassRef:init()
    display.nodeRemoveForNotHit(self._ui.Panel, function()
		self:closeView()
	end)
    self._scrollView = self._ui.ScrollView
    self._title = self._scrollView:getChildByName("title")
    self._content = self._scrollView:getChildByName("Text")

    self._lvWidth, self._lvHeight = self._scrollView:getContentSize2()
    self._titleWidth, self._titleHeight = self._title:getContentSize2()
    self._contentWidth, self._contentHeight = self._content:getContentSize2()
    self._title:setTextAreaSize(cc.size(self._titleWidth, 0))
    self._content:setTextAreaSize(cc.size(self._contentWidth, 0))
    
    self._curPage = 1
    self:refreshPageNum()
 
    self.onScrollEvt = function(sender, evenType)          
        local posY = self._scrollView:getInnerContainer():getPositionY()
        local height = self._scrollView:getInnerContainerSize().height
        local minPosY = self._lvHeight - height   
        self._curPage = math.max(math.floor((posY-minPosY) / self._lvHeight)+1, 1)
        self:refreshPageNum()
    end
    self._scrollView:setDirection(ccui.ScrollViewDir.vertical)
    self._scrollView:addEventListener(self.onScrollEvt)  
end

function ClassRef:refresh(data)
    if data then
        local eventId = data.eventId
        local playerInfo = data.newsInfo or {}
        local extraStr = (playerInfo[1] and playerInfo[1].BaseInfo) and playerInfo[1].BaseInfo.name or "" -- 目前只需要玩家名字
        local conf = GD:getNewsPaperEventById(eventId)
        if not conf then return end
        
        self._title:setString(string.format(L(conf.title1), extraStr))
        self._content:setString(string.format(L(conf.describe)))      
        local verticalTlt = self._title:getVirtualRendererSize()      
        local verticalText = self._content:getVirtualRendererSize()
        self._title:setTextAreaSize(verticalTlt)
        self._content:setTextAreaSize(verticalText)
        local listViewContainer = self._scrollView:getInnerContainer()
        local containerH = math.max(verticalText.height+verticalTlt.height, self._lvHeight)
        listViewContainer:setContentSize(cc.size(self._lvWidth, containerH))
        self._title:setPositionY(containerH)
        self._content:setPositionY(containerH - verticalTlt.height)
        self._scrollView:jumpToTop()
    end
end

function ClassRef:refreshPageNum()
    self._ui.page_num:setString("-"..self._curPage.."-")
end

return ClassRef